<template>
  <div>
    <Card class="marginB10 headTips teS_CL" dis-hover>
      <div class="relative">
        <Button
          :type="(order_type==0)? 'primary':'text'"
          size="large"
          class="marginR10 hteBtn"
          @click="bigNavFun(0)"
        >全部</Button>
        <Button
          :type="(order_type==1)? 'primary':'text'"
          size="large"
          class="marginR10 hteBtn"
          @click="bigNavFun(1)"
        >专利订单</Button>
        <Button
          :type="(order_type==2)? 'primary':'text'"
          size="large"
          class="marginR10 hteBtn"
          @click="bigNavFun(2)"
        >商标订单</Button>
        <Button
          :type="(order_type==3)? 'primary':'text'"
          size="large"
          class="marginR10 hteBtn"
          @click="bigNavFun(3)"
        >版权订单</Button>
        <Button
          :type="(order_type==5)? 'primary':'text'"
          size="large"
          class="marginR10 hteBtn"
          @click="bigNavFun(5)"
        >商标分析</Button>
        <Button type="default" class="pullRight relative"  @click="sX_fun" size="small" shape="circle" style="top:3px;">
          <Icon type="ios-arrow-down" class="cCCC" v-show="!show" size="18" />
          <Icon type="ios-arrow-up" class="cCCC" v-show="show" size="18" />
          筛选条件
        </Button>
      </div>

      <Sx :show="show" :order_type="order_type" ref="Sx" @pageTo1="pageTo1" @goFun="getList" @initL="initMq" />
    </Card>
    <Card dis-hover class="teS_CL">
      <div class="marginB10">
        <span class="inlineBlock relative f_14" style="top:1px;">状态：</span>
        <RadioGroup v-model="typeTt" @on-change="tableNavFun">
          <Radio :label="0">
            <span>全部（{{tX0}}）</span>
          </Radio>
          <Radio :label="1">
            <span>待付款（{{tX1}}）</span>
          </Radio>
          <Radio :label="2">
            <span>服务中（{{tX2}}）</span>
          </Radio>
        </RadioGroup>
      </div>
      <Table
        :data="tableData"
        :columns="columns"
        stripe
        border
        :size="!isHome?'default':'small'"
        class="marginB10"
        :loading="loading"
        :height="height"
        @on-sort-change="paiXuGo"
      ></Table>
      <Page
        :total="total"
        :current="page"
        :page-size="pageSize"
        :page-size-opts="$app.pageAll.pageSizeOpts"
        show-total
        show-sizer
        show-elevator
        size="small"
        @on-change="changePage"
        @on-page-size-change="pSizeChange"
      ></Page>
    </Card>

    <!-- 下单成功支付框 -->
    <PayBox ref="payBox" :noOrderList="true" />

    <!-- 简单的详情框 type 8专利 9商标 8和9都不显示Card -->
    <Temp ref="Temp" :type="tempType" />

    <!-- 退款弹框 -->
    <Modal
      :mask-closable="false"
      v-model="tuiBox"
      title="退款详情"
      :footer-hide="true"
      @on-visible-change="vxU"
    >
      <div class="f_16 c999">
        <div class="lineH2" v-show="parseFloat(tuiObj.refund_balance)>0">已退款余额：<span class="f_20 cRed f_bold">{{parseFloat(tuiObj.refund_balance)}}</span> 元</div>
        <div class="lineH2" v-show="parseFloat(tuiObj.refund_currency)>0">已退款猫币：<span class="f_20 cRed f_bold">{{parseFloat(tuiObj.refund_currency)}}</span></div>
        <div class="lineH2" v-show="parseFloat(tuiObj.refund_wx)>0">已退微信：<span class="f_20 cRed f_bold">{{parseFloat(tuiObj.refund_wx)}}</span> 元</div>
      </div>
    </Modal>
  </div>
</template>

<style lang="less" scoped>
.headTips {
  .hteBtn {
    height: 30px;
    line-height: 29px;
  }
}
</style>
<style >
.teS_CL .ivu-card-body{padding:10px 15px;}
</style>

<script>
import Temp from "@$@/_service/temp";
import PayBox from "@$@/_service/payBox";
import Sx from "./sx";
export default {
  name: "orderLi",
  components: { Sx, PayBox, Temp },
  data() {
    //获取路由传参
    let {
      $route: { 
        query:{
          order_type,
          page,
          pageSize,
          order_sort,
        }
      }
    } = this;

    /****
     * 0全部,1专利申请,2商标注册,3版权订单(软著),4版权订单(美术作品),5商标分析报告-单次，
     * 6商标分析报告-包年,7服务大厅商标订单,8服务大厅专利订单,9补差价那个,10版权购买无流程,0或不传为全部
     ****/
    if(order_type == 3 || order_type == 4 || order_type == 10) order_type = 3;
    if(order_type == 5 || order_type == 6) order_type = 5;
    if(order_type == 1 || order_type == 8) order_type = 1;
    if(order_type == 2 || order_type == 7) order_type = 2;

    let data = {
      //数据
      tableData: [],
      //栏位
      columns: []
    };
    //分页相关
    let pageTool = {
      loading: true,
      total: 0 //总页数
    };

    //请求相关参数
    let tool = {
      tempType: "",
      //获取所有订单类表 0全部,1待付款,2服务中
      typeTt: 0,

      // 从最近到最远
      // 从最远到最近
      order_sort: order_sort||'desc', //asc 或 desc

      order_type:parseInt(order_type)||0, //订单类型,0全部,1专利申请,2商标注册,3版权订单(软著+美术作品),5商标分析报告-单次，6商标分析报告-包年
      page:parseInt(page)||1, //当前页码
      pageSize: parseInt(pageSize)||$app.pageAll.pageSize, //分页条数，最低15条

      // 条数:0全部,1待付款,2已付款
      tX0: 0,
      tX1: 0,
      tX2: 0
    };

    // 筛选条件工具类
    let sX = {
      show: false,
      tuiBox:false,//退款详情弹框
      tuiObj:{},
    };
    return Object.assign(data, pageTool, tool, sX);
  },
  created() {},
  mounted() {
    this.$refs["Sx"].cY(this.order_type); //往子组件传值
    //首次更新数据
    this.getList();
  },
  //表格高度；是否首页
  props: ["height", "isHome"],
  methods: {
    //退款详情框
    vxU(buEr){
      if(!buEr){
        //初始化数据
        this.tuiObj={};
      }
    },
    //退款详情查看
    tuiKuan(obj){
      this.tuiObj=obj;//赋值
      this.tuiBox=true;//显示弹框
    },
    //头部选项卡切换
    tableNavFun() {
      this.pageTo1();
      this.$nextTick(() => {
        this.getList();
      });
    },

    paiXuGo(obj) {
      let { column, key, order } = obj;
      let { order_sort } = this;

      if (order_sort == "asc") {
        this.order_sort = "desc";
      } else {
        this.order_sort = "asc";
      }

      this.$nextTick(() => {
        this.getList();
      });
    },
    //ajax回来的数据
    getList() {
      let that = this;

      let { typeTt: type, order_type, page, pageSize } = that;

      //清空数据
      that.loading = true; //转转
      // that.tableData=[];//数据
      that.columns = []; //栏目

      //栏位数据条数 初始化
      this.tX0 = 0;
      this.tX1 = 0;
      this.tX2 = 0;

      let obj = {
        type,
        order_type,
        page,
        pageSize
      };

      //多条件筛选的额外参数
      let objTool = this.objToolFun();

      Object.assign(obj, objTool); // 对象合并

      // console.log('最终请求obj = ',obj)
      //获取所有订单类表
      //type:0全部,1待付款,2已付款
      //order_type:订单类型,1专利申请,2商标注册,3版权订单(软著+美术作品),5商标分析报告,0或不传为全部
      $ajax.orderAllList(obj,resData => {
        
          //全部 待付款 服务中
          this.tX0 = resData.all;
          this.tX1 = resData.not_pay;
          this.tX2 = resData.payed;

          //数据
          that.tableData = resData.list;
          //栏目
          that.columns = $app.tableCol(that);

          //分页
          that.total = resData.total;
          that.loading = false;
        },
        resData => {
          that.loading = false;
        }
      );
    },

    //多条件赛选
    objToolFun() {
      let { order_type, order_sort } = this;

      //取子组件的东西
      let { inp, fruit, optZ, dateStart, dateEnd } = this.$refs["Sx"];

      console.log();

      // 判断开始时间
      let dStart = dateStart.valueOf() / 1000;
      if ($app.judgeData(dStart)) {
        dStart = $app.dateTran($app.formatTime(2, dStart));
      } else {
        dStart = $app.dateTran($app.timeMin);
      }

      //结束时间判断
      let dEnd = "";
      if (typeof dateEnd === "string") {
        dEnd = $app.dateTran(dateEnd);
      } else {
        let vEnd = dateEnd.valueOf() / 1000;
        dEnd = $app.dateTran($app.formatTime(2, vEnd));
      }

      let out = {
        order_sort,
        order_key: "created_at",
        order_date: dStart + "-" + dEnd
      };

      if (order_type == 1) {
        $app.judgeData(fruit) && (out.patent_state = JSON.stringify(fruit)); //状态
      } else if (order_type == 2) {
        $app.judgeData(fruit) && (out.tm_state = JSON.stringify(fruit)); //状态
      } else if (order_type == 3) {
        $app.judgeData(fruit) && (out.cp_state = JSON.stringify(fruit)); //状态
      }

      $app.judgeData(inp) && (out[optZ] = inp); //文本框筛选条件

      console.log("订单 类型", order_type, "\n获得多条件查询 = ", out);

      return out;
    },

    // 筛选框
    sX_fun() {
      this.show = !this.show;
    },

    //改变页码
    changePage(num) {
      this.page = num;
      this.$nextTick(() => {
        this.getList();
      });
    },
    //改变 每页几条
    pSizeChange(pSize) {
      this.pageSize = pSize;
      this.pageTo1();
      this.$nextTick(() => {
        this.getList();
      });
    },

    bigNavFun(i) {
      this.$refs["Sx"].cY(i); //往子组件传值
      this.order_type = i;
      this.initMq();

      this.$nextTick(() => {
        this.getList();
      });
    },
    //初始化重置排序 和付款状态
    initMq() {
      this.pageTo1();
      this.typeTt = 0;
      this.order_sort = "desc"; //排序初始化
    },
    btnPay(obj) {
      let that = this;
      $ajax.orderInfo(obj.id,resData=>{
        //订单状态,0已取消,1已下单,2已支付
        switch(parseInt(resData.state)){
          case 0:
            that.$Message.error({content:'订单已取消，无需支付'})
            that.getList();//刷新列表数据
          break;
          case 1:
            $app.payBoxShow(that, obj.id, {
              payFun: () => {
                
                that.getList();//刷新列表数据
              }
            });
          break;
          case 2:
            that.$Message.success({content:'订单已支付，无需重复支付'})
            that.getList();//刷新列表数据
          break;
        }
      });
      
    },
    pageTo1(){
      this.page = 1;
    },
    //查看详情
    goInfo(params) {
      let that = this;

      let {
        row,
        row: { id: order_id, order_type, goods_type, goods_name }
      } = params;

      console.log("查看详情 = ", row);

      let name = "";
      switch (parseInt(order_type)) {
        case 1: //专利
          name = "patentDetails";
          break;
        case 2: //商标
          name = "tmDetails";
          break;
        case 3: //软著
          name = "rjDetails";
          break;
        case 4: //美术
          name = "msDetails";
          break;
      }

      if (
        order_type == 1 ||
        order_type == 2 ||
        order_type == 3 ||
        order_type == 4
      ) {
        // 连接跳转另一个页面详情
        this.lingInfo(name,order_id);
      } else if (order_type == 7) {
        //商标
        //传值给Temp组件，tempType= 8专利 或 9商标，服务大厅 的购买选项卡，就不会显示
        that.tempType = 9;

        let {
          row,
          row: {
            trademark_info
          }
        } = params;
        if($app.judgeData(trademark_info)){
          let {
            apply_number, name, remark 
          }=trademark_info;

          that.$refs["Temp"].trademark_name = name;
          that.$refs["Temp"].register_number = apply_number;
          that.$refs["Temp"].remark = remark;

          that.wuLiuCc(goods_type,goods_name,order_id,row); //无流程订单查看详情

        }else{
          $app.toast('商标订单，旧数据已失效',false);
        }

        
      } else if (order_type == 8) {
        //专利
        //传值给Temp组件，tempType= 8专利 或 9商标，服务大厅 的购买选项卡，就不会显示
        that.tempType = 8;

        let {
          row,
          row: {
            remark,
            goods_type,

            patent_info,
            state//订单状态,0已取消,1已下单,2已支付
          }
        } = params;

        if($app.judgeData(patent_info)){

          let {
            name,
            patent_number,
            applicant_name_list,
            zip_file_path,
            write_patent_type,//0非打包非撰写服务，大于0撰写服务，1发明，2实用新型，3外观
            write_only//0撰写+代缴=要去补充资料，1纯撰写，无需补充资料
          }=patent_info;

          if($app.is103_108(goods_type)){
            that.lingInfo('patentThird',order_id);
          }else if($app.is130_132(goods_type)){
            that.lingInfo('patentThird',order_id);
          }else{
            //无流程
            that.$refs["Temp"].patent_name = name;
            that.$refs["Temp"].patent_number = patent_number;
            that.$refs["Temp"].applicant_name_list = applicant_name_list;
            that.$refs["Temp"].remark = remark;
            that.$refs["Temp"].boxObj.zip_file_path = zip_file_path;
            this.wuLiuCc(goods_type,goods_name,order_id,row); //无流程订单查看详情
          }

          
        }else{
          $app.toast('专利订单，旧数据已失效',false);
        }
        

      } else if (order_type == 5 || order_type == 6) {
        //商标分析购买报告，5单次，6包年
        let {
          row: {
            tm_report: { id: report_id }
          }
        } = params;
        $app.openTmReport(report_id); //打开新页面查看商标购买报告
      }
    },
    //无流程订单查看详情
    wuLiuCc(goods_type,goods_name,order_id,row) {
      let that=this;
      
      $app.loading();
      // 临时解决办法，系统的设计有问题
      setTimeout(() => {
        $app.loading('hide');
        
        that.$refs["Temp"].order_id = order_id;
        that.$refs["Temp"].boxObj.zhongObj = {
          //zhongObj对象只用了以下4个数据
          goods_type,
          price: row.order_money,
          official_fee: row.official_fee,
          service_fee: row.service_fee
        };
        that.$refs["Temp"].away(goods_type, goods_name);
      }, 200);
      
    },

    // 连接跳转另一个页面详情
    lingInfo(name,order_id){
      // 页面跳转
      this.$router.push({
        name,
        query: { order_id }
      });
    },
    

    // 专利撰写填写资料
    tianXieBook(row){
      let that=this;

      let {id:order_id}=row;

      //专利详情
      $ajax.itwPT(order_id,resData=>{
         // 有专利对象 && 没有申请人 && 没有发明人
        if($app.judgeData(resData) && (!$app.judgeData(resData.inventor_name_list))  && (!$app.judgeData(resData.applicant_name_list))){
          //订单详情
          $ajax.orderInfo(order_id,resData=>{
            $app.goEditDB(order_id,resData);//页面跳转 补充资料
          });
        }else{
          that.$Message.success({content:'资料已补充，无需重复补充'})
          that.getList();//刷新列表数据
        }
      });
    },

    // 删除订单
    delOrderGo(obj){
      let that=this;
      //订单详情
      $ajax.orderInfo(obj.id,resData=>{
        if(resData.state==2){
          that.$Message.success({content:'订单已付款，删除无效'})
          that.getList();//刷新列表数据
        }else{
          $app.delOrderSure(obj.id, () => {
            that.getList();//重新回来数据
          });
        }
        
      });
    }
  }
};
</script>

